from flask import Flask
from urllib.request import urlopen
from bs4 import BeautifulSoup
import json

import config
from models import *

app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)


def deal_title(s):
    res = str(s)
    res = res.replace("<title>", "")
    res = res.replace("</title>", "")
    res = res.replace("_百度百科", "")
    return res


def deal_url(s):
    res = str(s)
    res = res.replace("/item/", "")
    return res


@app.route("/init")
def init():
    db.create_all()
    first_entry = Entry(url = "%E7%AE%97%E6%B3%95")
    db.session.add(first_entry)
    db.session.commit()
    print("初始化完成")
    return "初始化完成"


@app.route("/run/<b>/<e>")
def run(b, e):
    now_id = int(b)
    end_id = int(e)
    while now_id <= end_id:
        now_entry = Entry.query.filter(Entry.id == now_id).first()
        edge_list = []
        now_id += 1
        html = urlopen("https://baike.baidu.com/item/" + now_entry.url, timeout = 10)
        bsobj = BeautifulSoup(html.read(), "html.parser")
        if now_entry.title is None:
            now_entry.title = deal_title(bsobj.title)
            db.session.commit()
        paras = bsobj.find_all("div", {"class": "para"})
        for para in paras:
            tagas = para.find_all("a", {"target": "_blank"})
            for taga in tagas:
                url = str(taga.attrs["href"])
                if url.find("/item") != 0:
                    continue
                url = deal_url(url)
                res = Entry.query.filter(Entry.url == url).first()
                if res is None:
                    ins = Entry(url = url)
                    db.session.add(ins)
                    db.session.commit()
                    res = Entry.query.filter(Entry.url == url).first()
                edge_list.append(res.id)
        now_entry.edge = json.dumps(edge_list)
        db.session.commit()
    return "complete!"


if __name__ == "__main__":
    app.run()
